package cn.scw.project.system.service.impl;

import cn.scw.common.utils.DateUtils;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.framework.aspectj.lang.annotation.DataScope;
import cn.scw.project.system.domain.SysDealer;
import cn.scw.project.system.domain.SysUser;
import cn.scw.project.system.domain.SysUserBrokerage;
import cn.scw.project.system.mapper.SysUserBrokerageMapper;
import cn.scw.project.system.service.ISysDealerService;
import cn.scw.project.system.service.ISysUserBrokerageService;
import cn.scw.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 每日佣金Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class SysUserBrokerageServiceImpl implements ISysUserBrokerageService {
    @Autowired
    private SysUserBrokerageMapper sysUserBrokerageMapper;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysDealerService iSysDealerService;

    /**
     * 查询每日佣金
     *
     * @param id 每日佣金ID
     * @return 每日佣金
     */
    @Override
    public SysUserBrokerage selectSysUserBrokerageById(Long id) {
        return sysUserBrokerageMapper.selectSysUserBrokerageById(id);
    }

    /**
     * 查询每日佣金列表
     *
     * @return 每日佣金
     */
    @Override
    public List<SysUserBrokerage> selectSysUserBrokerageList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return sysUserBrokerageMapper.selectSysUserBrokerageList(user.getUserId());
    }

    /**
     * 新增每日佣金
     *
     * @return 结果
     */
    @Override
    public int insertSysUserBrokerage(SysUserBrokerage sysUserBrokerage) {
        return sysUserBrokerageMapper.insertSysUserBrokerage(sysUserBrokerage);
    }

    @Transactional
    @Override
    public void addSysUserBrokerageMoney(String facilityCode, double money) {
        SysUser user = iSysUserService.selectUserByFacilityCode(facilityCode);
        if (user == null)
            return;
        SysUserBrokerage brokerage = this.selectSysUserBrokerageByToDay(user);
        brokerage.setMoney(brokerage.getMoney() + money);

        //修改经销商今日佣金
        SysDealer dealer = iSysDealerService.selectSysDealerByUserId(user.getUserId());
        if (dealer != null) {
            dealer.setBrokerage(dealer.getBrokerage() + money);
        }

        sysUserBrokerageMapper.insertSysUserBrokerage(brokerage);
    }


    /**
     * 修改每日佣金
     *
     * @param sysUserBrokerage 每日佣金
     * @return 结果
     */
    @Override
    public int updateSysUserBrokerage(SysUserBrokerage sysUserBrokerage) {
        return sysUserBrokerageMapper.updateSysUserBrokerage(sysUserBrokerage);
    }

    /**
     * 批量删除每日佣金
     *
     * @param ids 需要删除的每日佣金ID
     * @return 结果
     */
    @Override
    public int deleteSysUserBrokerageByIds(Long[] ids) {
        return sysUserBrokerageMapper.deleteSysUserBrokerageByIds(ids);
    }

    /**
     * 删除每日佣金信息
     *
     * @param id 每日佣金ID
     * @return 结果
     */
    @Override
    public int deleteSysUserBrokerageById(Long id) {
        return sysUserBrokerageMapper.deleteSysUserBrokerageById(id);
    }

    public SysUserBrokerage selectSysUserBrokerageByToDay(SysUser user) {
        Date date = DateUtils.getDay(new Date(),DateUtils.start);
        SysUserBrokerage sysUserBrokerage = sysUserBrokerageMapper.selectSysUserBrokerageByUserAndDate(user.getUserId(), date);
        if (sysUserBrokerage == null) {
            sysUserBrokerage = new SysUserBrokerage(user.getUserId(), 0.0, date, false);
            this.insertSysUserBrokerage(sysUserBrokerage);
        }
        return sysUserBrokerage;
    }

    @Override
    public SysUserBrokerage selectSysUserBrokerageByToDay() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return this.selectSysUserBrokerageByToDay(user);
    }

    @Transactional
    @Override
    public void BrokeragePay() {
        Date date = new Date();
        date = DateUtils.getDay(DateUtils.addDay(date, -1), DateUtils.end);

        List<SysUserBrokerage> list = sysUserBrokerageMapper.selectByDateAndIsPay(date, false);
        for (SysUserBrokerage brokerage : list) {
            SysUser user = iSysUserService.selectUserById(brokerage.getUserId());
            user.setMoney(user.getMoney() + brokerage.getMoney());
            iSysUserService.updateUserInfo(user);
            brokerage.setPay(true);
            sysUserBrokerageMapper.updateSysUserBrokerage(brokerage);
        }
        // 重置经销商佣金
        iSysDealerService.brokerageReset();
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public double countAll(SysUser user) {
        return sysUserBrokerageMapper.countAll(user);
    }

    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public double countAllByDealer(SysUser user) {
        return sysUserBrokerageMapper.countAllByDealer(user);
    }
}
